"use strict";
/* Controllers */
app.controller("kpiReportAddController", ['$scope', '$location', '$rootScope', '$routeParams', 'kpiReportAddService', function ($scope, $location, $rootScope, $routeParams, kpiReportAddService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.p.groupId = $routeParams.groupId;
    vm.p.metricId = $routeParams.metricId;
    vm.p.id = $routeParams.id;

    vm.p.dimension='all';

    var errorFn = function (result) {
        alert(result);
    };

    $scope.addReport = function () {
        var vm = $scope.vm;
        var params = vm.p;
        var procFn = function (response) {
            if (response.success && response.data) {
                alert("执行成功！")
            } else {
                alert(response.msg);
            }
        };
        kpiReportAddService.addReport(params, procFn, errorFn);
    };

    $scope.getReport = function () {
        var vm = $scope.vm;
        var params = {
            id: vm.p.id
        };
        var procFn = function (response) {
            if (response.success && response.data) {
                //vm.p = JSON.stringify(response.data);
                vm.p = response.data;
            } else {
                alert(response.msg);
            }
        };
        kpiReportAddService.getReport(params, procFn, errorFn);
    };
    if(vm.p.id!=0) {
        console.log(vm.p.id);
        $scope.getReport();
    }
}]);


/* Services */
app.service('kpiReportAddService', ['ajaxService', function (ajaxService) {

    this.addReport = function (metrics, successFunction, errorFunction) {
        ajaxService.AjaxPost(metrics, "/star/api/basic-service/v1/kpi/setReport.do", successFunction, errorFunction);
    };

    this.getReport = function (metrics, successFunction, errorFunction) {
        ajaxService.AjaxPost(metrics, "/star/api/basic-service/v1/kpi/getReport.do", successFunction, errorFunction);
    };

}]);

